﻿//
// On tente un truc : creer une WizardStep en live si l'envoit d'un message de creation
// est configurer ca ne marche pas comme on veut mais une vois une chose etrange :
// HeaderText = "Envoyez votre demande d'enregistrerent" apparait en haute de la CreateUserWizard1 box
// il semblerait que je n'utlise pas le Wizard comme il faut mais bon ... j'ai pas le temps
//
// Impossible d'utiliser le putain d'objet de merde de System.Net.Mail.MailMessage
// l'ancien fonctionne mieux !!! 
// C'est peut etre du au format du mail utf8 ou texte qui n'a pas ete configurer dans le code...
//
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Mail;
//using System.Net;
//using System.Net.Mail;
using System.Net.Configuration;

public partial class CreateUserWizardPage : PageBase
{
    static string MessageFromSmtpServer = "";

    protected void Page_Load( object sender, EventArgs e )
    {
        CreateUserWizard1.EmailRegularExpressionErrorMessage = Resources.labels.FormatAdresseNonValide;
    }

    protected void CreateUserWizard1_CreatedUser( object sender, EventArgs e )
    {
        if ( Config.MailOnUserCreation == "true" )
        {
            MessageFromSmtpServer = "";

            MailMessage mail = new MailMessage();
            mail.From = CreateUserWizard1.Email;
            mail.To = Config.MailTo;

            SmtpMail.SmtpServer = Config.MailServerSmtp;
            if ( Config.SafeConfigString( "appSettings", "mailServerAuthenticate", string.Empty ) == "1" )
            {
                mail.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1" );
                mail.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/sendusername", Config.SafeConfigString( "appSettings", "mailServerUser", string.Empty ) );
                mail.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/sendpassword", Config.SafeConfigString( "appSettings", "mailServerPassword", string.Empty ) );
            }

            mail.BodyFormat = MailFormat.Text;
            mail.Subject = Config.MailSubject;
            mail.Body = Config.MailBody;

            try
            {
                SmtpMail.Send( mail );
                MessageFromSmtpServer += Config.MailConfirmation;
            }
            catch ( System.Web.HttpException ex )
            {
                MessageFromSmtpServer += Resources.labels.ErreurEnvoitConfirmation + " <br>";
                MessageFromSmtpServer += ex.Message;
            }
            catch ( Exception ex )
            {
                MessageFromSmtpServer += Resources.labels.ErreurEnvoitConfirmation + " <br>";
                MessageFromSmtpServer += ex.Message;
            }

            //if ( Config.MailOnUserCreation == "true" )
            //{
            //    MailMessage mail = new MailMessage();

            //    //MailAddress from = new MailAddress( CreateUserWizard1.Email );
            //    MailAddress from = new MailAddress( "licensing@sodevlog.com" );
            //    MailAddress to = new MailAddress( Config.MailTo );
            //    mail.From = from;
            //    mail.To.Add( to );
            //    mail.Subject = Config.MailSubject;
            //    mail.Body = Config.MailBody;

            //    SmtpClient smptClient = new SmtpClient( Config.MailServerSmtp );
            //    //smptClient.UseDefaultCredentials = true;
            //    NetworkCredential nwc = new System.Net.NetworkCredential( "licensing@sodevlog.com", "zazaza", "www.sodevlog.com" );
            //    smptClient.UseDefaultCredentials = false;
            //    smptClient.Credentials = nwc;
            //    //smptClient.Credentials
            //    try
            //    {
            //        smptClient.Send( mail );
            //    }
            //    catch ( SmtpFailedRecipientsException ex )
            //    {
            //        for ( int i = 0; i < ex.InnerExceptions.Length; i++ )
            //        {
            //            SmtpStatusCode status = ex.InnerExceptions[ i ].StatusCode;
            //            if (    status == SmtpStatusCode.MailboxBusy 
            //                 || status == SmtpStatusCode.MailboxUnavailable )
            //            {
            //                MessageFromSmtpServer = "Delivery failed - retrying in 5 seconds.";
            //                System.Threading.Thread.Sleep( 5000 );
            //                smptClient.Send( mail );
            //            }
            //            else
            //            {
            //                MessageFromSmtpServer = "Failed to deliver message : " + ex.FailedRecipient[ i ];
            //            }
            //        }
            //    }
            //    catch ( SmtpException ex )
            //    {
            //        MessageFromSmtpServer += "Erreur : ";
            //        MessageFromSmtpServer += ex.Message;
            //    }
            //    catch ( HttpException ex )
            //    {
            //        MessageFromSmtpServer  = "Erreur : <br>";
            //        MessageFromSmtpServer += ex.Message;
            //    }
            //    catch ( Exception ex )
            //    {
            //        MessageFromSmtpServer = "Erreur : <br>";
            //        MessageFromSmtpServer += ex.Message;
            //    }
            //}

            CreateUserWizard1.CompleteSuccessText += "<br>" + MessageFromSmtpServer;
        }
    }
    protected void CreateUserWizard1_ContinueButtonClick( object sender, EventArgs e )
    {
        int i = 5;
    }
    protected void CreateUserWizard1_SendingMail( object sender, MailMessageEventArgs e )
    {
        int i = 35;
    }
    protected void CreateUserWizard1_NextButtonClick( object sender, WizardNavigationEventArgs e )
    {
        int i = 654;
    }
}
